package bulletbehavior 
{
	import org.flixel.FlxG;
	/**
	 * ...
	 * @author morgan
	 */
	public class RotateBehavior extends BulletBehavior
	{
		public var angle:Number;
		public var degreesPerSecond:Number;
		public function RotateBehavior(angle:Number, degreesPerSecond:Number = 0) 
		{
			this.angle = angle;
			this.degreesPerSecond = degreesPerSecond;
		}
		
		public function rotate(b:Bullet, a:Number):void
		{
			var oldAngle:Number = Math.atan2(b.velocity.y, b.velocity.x);
			var speed:Number = Math.sqrt(b.velocity.x * b.velocity.x + b.velocity.y * b.velocity.y);
			var newAngle:Number = oldAngle + a * 3.14159 / 180;
			b.velocity.x = Math.cos(newAngle) * speed;
			b.velocity.y = Math.sin(newAngle) * speed;
		}
		
		override public function onActivate(b:Bullet):void 
		{
			super.onActivate(b);
			rotate(b, angle);
		}
		
		override public function onUpdate(b:Bullet):void 
		{
			super.onUpdate(b);
			if (degreesPerSecond != 0)
			{
				rotate(b, degreesPerSecond * FlxG.elapsed);
			}
		}
		
	}

}